package com.passive.lock.controller;

import com.passive.lock.model.vo.TUserVo;
import com.passive.lock.service.user.UserService;
import com.passive.lock.utils.Config;
import com.passive.lock.utils.ResourcesUtil;
import com.passive.lock.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("")
public class LoginController extends BaseController {

	@Autowired
	private UserService userService;
	
	/**
	 * 用户登录
	 *
	 * @param model 
	 * @returns
	 */
	@RequestMapping(value = { "", "/login" })
	public String login(Model model) {
		
		return "Forward:index.html";
	}

	@RequestMapping(value = "/userlogin", method = RequestMethod.POST)
	@ResponseBody
	public Result userLogin(@RequestBody TUserVo uservo,HttpServletRequest request, HttpServletResponse response) {
		try {
			Result result = userService.userLogin("asdasdasdas",uservo, request, response);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	/**
	 * 退出
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpSession session) {
		// session过期
		session.invalidate();
		return "redirect:login.action";
	}
	
}
